// @algorithm @lc id=98 lang=cpp 
// @title validate-binary-search-tree


// @test([2,1,3])=true
// @test([5,1,4,null,null,3,6])=false
class Solution {
    int64_t last = int64_t(INT_MIN) - 1;
    bool InOrder(TreeNode * node){
        if(!node) return true;
        if(node->left && !InOrder(node->left)) return false;
        if(node->val <= last) return false;
        last = node->val;
        if(node->right) return InOrder(node->right);
        return true;
    }
public:
    bool isValidBST(TreeNode* root) {
        return InOrder(root);
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */